﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Timers;
using System.Windows.Forms;
using System.Configuration;

namespace HobSoft.SoccerBet2014.Server.SiteManagerSchedule
{
    using Domain.Factory;
    using Domain.Models;
    public class BfScheduleManager
    {
        private readonly IBfManagerFactory bfManagerFactory;
        protected BindingList<BfManagerView> AccountBfManagerCollection;
        protected Dictionary<string, BfManagerView> AccountBfManagerDictionary;
        protected IList<BFAccount> BfAccounts;
        protected DataGridView bindDataGrid;
        protected System.Timers.Timer timer;
        protected bool EnableCheck = false;
        public BfScheduleManager(IBfManagerFactory bfMgFactory)
        {
            bfManagerFactory = bfMgFactory;
            AccountBfManagerCollection = new BindingList<BfManagerView>();
            timer = new System.Timers.Timer();
            timer.Elapsed += Timer_Elapsed;
            timer.Interval = 2000;
        }

        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            var query = AccountBfManagerDictionary.Values.Where(v => v.OnSignup).OrderBy(v => v.LastCheckTime);
            var actView = query.FirstOrDefault();
            if (actView != null)
            {
                timer.Stop();
                actView.CheckMarketClose().ContinueWith(t =>
                {
                    if (EnableCheck)
                        timer.Start();
                });
            }
        }

        public void Initialize(IList<BFAccount> accounts)
        {
            BfAccounts = accounts;
            AccountBfManagerCollection.Clear();
            AccountBfManagerDictionary.Clear();
            var certFile = ConfigurationManager.AppSettings["BfGlobalCertificateFile"];
            if (string.IsNullOrEmpty(certFile))
                return;
            foreach (var act in BfAccounts)
            {
                if (AccountBfManagerDictionary.ContainsKey(act.Account))
                    continue;
                var managerService = bfManagerFactory.CreateOrGet(act);
                var managerView = new BfManagerView(act, managerService);
                managerView.CertificateFile = certFile;
                AccountBfManagerCollection.Add(managerView);
                AccountBfManagerDictionary.Add(act.Account, managerView);
            }
        }

        public virtual void StartCheck()
        {
            EnableCheck = true;
            timer.Start();
        }

        public virtual void StopCheck()
        {
            EnableCheck = false;
            timer.Stop();
        }

        public virtual void Signup()
        {
            foreach (var actView in AccountBfManagerDictionary.Values)
            {
                actView.Signup();
            }
        }

        public virtual void BindToGridControl(DataGridView grid)
        {
            bindDataGrid = grid;
            grid.DataSource = AccountBfManagerCollection;
        }
    }
}
